﻿using AIStudio.Wpf.Controls;
using MonitorPrism_Practice.Commons;
using MonitorPrism_Practice.Mvvm;
using MonitorPrism_Practice.Services;
using MonitorPrism_Practice.ViewModel;
using MonitorPrism_Practice.ViewModels;
using MonitorPrism_Practice.ViewModels.SettingConfigViewModel;
using MonitorPrism_Practice.Views;
using MonitorPrism_Practice.Views.SettingConfig;
using Prism.Ioc;
using Prism.Mvvm;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace MonitorPrism_Practice
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : MyPrismApplication
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        //log.Error("错误", new Exception("发生了一个异常"));//错误
        //    log.Fatal("严重错误", new Exception("发生了一个致命错误"));//严重错误
        //    log.Info("信息"); //记录一般信息
        //    log.Debug("调试信息");//记录调试信息
        //    log.Warn("警告");//记录警告信息
        public App()
        {

        }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //TextEditorThemeHelper.SetCurrentTheme("Dark");

            //全局错误处理
            //System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//处理在Task上未处理的异常
        }

        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                var exception = e.Exception;
                if (exception != null)
                {
                    log.Error(exception);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                //ignore
            }
            //记录严重错误     
        }

        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            log.Error(e.Exception);
            Application.Current.Dispatcher.Invoke((Action)delegate ()
            {
                System.Windows.Forms.MessageBox.Show(e.Exception.Message);
            });
            e.Handled = true;//使用这一行代码告诉运行时，该异常被处理了，不再作为UnhandledException抛出了。           
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var exception = e.ExceptionObject as Exception;
                if (exception != null)
                {
                    log.Error(exception);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                //ignore
            }
            //记录严重错误                  

        }

        protected override Window CreateShell()
        {

            var window = Container.Resolve<MainWindow>();
            return window;
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<IPlcService>(() =>
            new CommunicationS7("S71500", "192.168.0.1", 0, 1));
            containerRegistry.RegisterSingleton<InterfacePlotViewModel>();
            containerRegistry.RegisterSingleton<LogEventService>();
            containerRegistry.RegisterSingleton<ReciveDataEventService>();
            containerRegistry.RegisterSingleton<WriteDataEventService>();
            containerRegistry.RegisterSingleton<InterfaceReciveDataViewModel>();
            containerRegistry.RegisterSingleton<ClassLiveCharts2Line>();
            containerRegistry.Register(typeof(IUndoRedoManager), typeof(UndoRedoManager));
            containerRegistry.Register(typeof(IkeyBoardMouseClickService), typeof(KeyBoardMouseClickManager));
        }
        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {

        }
        protected override void InitializeShell(Window shell)
        {
            base.InitializeShell(shell);
        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            var homePageModule = typeof(HomeModule);
            moduleCatalog.AddModule(new ModuleInfo()
            {
                ModuleName = homePageModule.Name,
                ModuleType = homePageModule.AssemblyQualifiedName,
                InitializationMode = InitializationMode.WhenAvailable
            });
        }
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
            ViewModelLocationProvider.SetDefaultViewModelFactory(new ViewModelResolver(() => Container)
         .UseDefaultConfigure()
         .ResolveViewModelForView);
            ViewModelLocationProvider.Register<MainWindow, MainWindowViewModel>();
            ViewModelLocationProvider.Register<InterfaceReciveDataView, InterfaceReciveDataViewModel>();
            ViewModelLocationProvider.Register<InterfacePropertiesView, InterfacePropertiesViewModel>();
            ViewModelLocationProvider.Register<InterfaceLoggerView, InterfaceLoggerViewModel>();
            ViewModelLocationProvider.Register<InterfacePlotView, InterfacePlotViewModel>();
            ViewModelLocationProvider.Register<InterfaceWriteDataView, InterfaceWriteDataViewModel>();
            ViewModelLocationProvider.Register<InterfaceLineConfigView, InterfaceLineConfigViewModel>();
            ViewModelLocationProvider.Register<AddReciveDataView, AddReciveDataViewModel>();
            ViewModelLocationProvider.Register<AddWriteDataView, AddWriteDataViewModel>();

        }

    }

}
